package pl.edu.agh.ssm.monitor.visualization.test;

import java.net.InetAddress;
import java.net.UnknownHostException;

import pl.edu.agh.ssm.monitor.data.RTSPPacket;
import pl.edu.agh.ssm.monitor.data.RTSPPacketType;
import pl.edu.agh.ssm.monitor.data.SessionConnection;
import pl.edu.agh.ssm.monitor.data.SessionMedia;
import pl.edu.agh.ssm.monitor.data.SessionMediaType;
import pl.edu.agh.ssm.monitor.data.SessionNode;
import pl.edu.agh.ssm.monitor.data.SessionNodeType;
import pl.edu.agh.ssm.monitor.visualization.NetGraph;

public class NetGraphVisualization implements Runnable {

	private NetGraph graph;
	
	public void setGraph(NetGraph graph) {
		this.graph = graph;
	}
	
	@Override
	public void run() {
		for (int i = 0; i < 3; ++i) {

			try {
				SessionNode n1 = new SessionNode(InetAddress.getLocalHost(),
						"1111");
				n1.setNodeType(SessionNodeType.RECEIVER);
				graph.newNode(n1);

				InetAddress address = InetAddress.getByName("onet.pl");
				SessionNode n2 = new SessionNode(address, "1111");
				n2.setNodeType(SessionNodeType.SENDER);
				graph.newNode(n2);

				try {
					Thread.sleep(500);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				address = InetAddress.getByName("google.pl");
				SessionNode n3 = new SessionNode(address, "1111");
				n3.setNodeType(SessionNodeType.SENDER_AND_RECEIVER);
				graph.newNode(n3);

				if (i == 0) {
					n3.setNodeType(SessionNodeType.UNKNOWN);
					graph.updateNode(n3);
				}
				
				if (i == 2) {
					n3.setNodeType(SessionNodeType.MULTIPLEXER);
					graph.updateNode(n3);
				}


				if (i == 1) {
					n3.setNodeType(SessionNodeType.SERVER);
					graph.updateNode(n3);
				}
				
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				SessionConnection c1 = new SessionConnection(n1, n2,
						InetAddress.getLocalHost(), 1234, "1111");
				SessionMedia m = new SessionMedia();
				m.setMediaType(SessionMediaType.AUDIO);
				c1.setMediaDesc(m);
				c1.addRtspPacket(generateRtspPacket());				
				
				graph.newSessionConnection(c1);

				try {
					Thread.sleep(500);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				SessionConnection c2 = new SessionConnection(n2, n3,
						InetAddress.getLocalHost(), 1234, "1111");
				m = new SessionMedia();
				m.setMediaType(SessionMediaType.VIDEO);
				c2.setMediaDesc(m);
				c2.addRtspPacket(generateRtspPacket());
				graph.newSessionConnection(c2);

				graph.removeSessionConnection(c2);
				
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				graph.newSessionConnection(c2);
				
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				SessionConnection c3 = new SessionConnection(n1, n3,
						InetAddress.getLocalHost(), 1234, "1111");
				m = new SessionMedia();
				m.setMediaType(SessionMediaType.OTHER);
				c3.setMediaDesc(m);
				c3.addRtspPacket(generateRtspPacket());
				graph.newSessionConnection(c3);
				
				SessionConnection c5 = new SessionConnection(n1, n3,
						InetAddress.getLocalHost(), 1234, "1111");
				m = new SessionMedia();
				m.setMediaType(SessionMediaType.AUDIO);
				c5.setMediaDesc(m);
				c5.addRtspPacket(generateRtspPacket());
				graph.newSessionConnection(c5);


				SessionConnection c4 = new SessionConnection(n1, n3, address, 5678, "1111");
				m = new SessionMedia();
				m.setMediaType(SessionMediaType.VIDEO);
				c4.setMediaDesc(m);
				c4.addRtspPacket(generateRtspPacket());
				graph.newSessionConnection(c4);
				
			} catch (UnknownHostException e) {
				e.printStackTrace();
			}
		}	
	}
	
	
	/*
	 * Jaka metoda, taka nazwa;P
	 */
	private RTSPPacket generateRtspPacket() {
		RTSPPacket packet = new RTSPPacket();
		
		int randNum = (int) (Math.random() * 11);
		
		switch (randNum) {
		case 0 : packet.setType(RTSPPacketType.PLAY); break;
		case 1 : packet.setType(RTSPPacketType.ANNOUNCE); break;
		case 2 : packet.setType(RTSPPacketType.DESCRIBE); break;
		case 3 : packet.setType(RTSPPacketType.GET_PARAMETER); break;
		case 4 : packet.setType(RTSPPacketType.OPTIONS); break;
		case 5 : packet.setType(RTSPPacketType.PAUSE); break;
		case 6 : packet.setType(RTSPPacketType.RECORD); break;
		case 7 : packet.setType(RTSPPacketType.REDIRECT); break;
		case 8 : packet.setType(RTSPPacketType.SET_PARAMETER); break;
		case 9 : packet.setType(RTSPPacketType.SETUP); break;
		case 10 : packet.setType(RTSPPacketType.TEARDOWN); break;
		case 11 : break;
		}
		
		
		return packet;
	}

}
